﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Pitaya_IDE
{
    public class CtrlLine : Label
    {
        private MainPanel mainPanel;
        private SubPanel leftPanel,rightPanel;
        private int this_width = 1;

        public SubPanel RightPanel { get => rightPanel; set => rightPanel = value; }
        public SubPanel LeftPanel { get => leftPanel; set => leftPanel = value; }

        public CtrlLine(MainPanel _mainPanel, SubPanel _leftPanel, SubPanel _rightPanel)
        {
            mainPanel= _mainPanel;
            leftPanel= _leftPanel;
            rightPanel= _rightPanel;

            BackColor = Color.FromArgb(48, 54, 61);

            Size = new Size(this_width, _mainPanel.Height);

            Cursor = Cursors.SizeWE;

            this.MouseDown += new MouseEventHandler(Ctrl_MouseDown);
            this.MouseUp += new MouseEventHandler(Ctrl_MouseUP);
            this.MouseLeave += new EventHandler(Ctrl_MouseLeave);
            this.MouseMove += new MouseEventHandler(Ctrl_MouseMove);
            this.MouseHover += new EventHandler(Ctrl_MouseHover);
        }

        public void RefLct()
        {
            Location = new Point(rightPanel.Left - this_width, 0);
            Size = new Size(this_width, mainPanel.Height);
        }

        private bool pussFlag = false;
        private double PixScl;  //每个像素占的比例
        Point pussPoint;
        private double OldLeftPropn, OldRightPropn;
        private void Ctrl_MouseUP(object sender, EventArgs e)
        {
            pussFlag = false;
            mainPanel.RefLct();
        }
        private void Ctrl_MouseDown(object sender, EventArgs e)
        { 
            pussFlag = true;
            PixScl = leftPanel.WidthPropn / leftPanel.Width;
            pussPoint = leftPanel.PointToClient(Control.MousePosition); //鼠标相对于pane左上角的坐标
            OldLeftPropn = leftPanel.WidthPropn;
            OldRightPropn = rightPanel.WidthPropn;
        }
        private void Ctrl_MouseLeave(object sender, EventArgs e)
        {
            this_width = 1;
            Location = new Point(rightPanel.Left - this_width, 0);
            Size = new Size(this_width, mainPanel.Height);
            BackColor = Color.FromArgb(48, 54, 61);
        }
        private void Ctrl_MouseHover(object sender, EventArgs e)
        {
            this_width = 7;
            Location = new Point(rightPanel.Left - (this_width - 1) / 2, 0);
            Size = new Size(this_width, mainPanel.Height);
            BackColor = Color.FromArgb(31, 111, 235);
        }
        private void Ctrl_MouseMove(object sender, MouseEventArgs e)
        {
            if (pussFlag)
            {
                Point panelPoint = leftPanel.PointToClient(Control.MousePosition); //鼠标相对于pane左上角的坐标

                int x = panelPoint.X;
                double tmpx = (x - pussPoint.X) * PixScl;
                
                double LeftMin,RightMin;
                LeftMin =  380 * PixScl - OldLeftPropn;
                RightMin = OldRightPropn - 380 * PixScl;

                if (tmpx < LeftMin && tmpx > RightMin)
                {
                    tmpx = 0;
                }
                else
                {
                    if (tmpx < LeftMin) tmpx = LeftMin;
                    if (tmpx > RightMin) tmpx = RightMin;
                }

                leftPanel.WidthPropn  = OldLeftPropn + tmpx;
                rightPanel.WidthPropn = OldRightPropn - tmpx;

                Location = new Point((int)(tmpx / PixScl) + pussPoint.X + leftPanel.Left, 0);

                leftPanel.RefreshLocation();
                rightPanel.RefreshLocation();
            }
        }
    }
}